//
//  NetInfoViewCell.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/4/13.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "NetInfoViewCell.h"

@interface NetInfoViewCell ()
{
    CGFloat space;
    UILabel  *_colorLabel;
    UILabel  *_numLabel;
    UILabel  *_nameLable;
    UILabel  *_percentLabel;
    
    UIBezierPath *_bezier ;
    CAShapeLayer *_shapLayer;
    CABasicAnimation *_strokeEndAnimation ;
}
@end

@implementation NetInfoViewCell


+ (CGFloat)cellHeight
{
    return 75.0f;
}
 + (instancetype)cellWithTabelView:(UITableView *)tableView
{
    static NSString *NetInfoViewCellID = @"NetInfoViewCellId";
    NetInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NetInfoViewCellID];
    if (cell == nil) {
        cell = [[NetInfoViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NetInfoViewCellID];
        [cell addSubviews];
    }
    return cell;
}

- (void)addSubviews
{
    space = [UIView getWidth:10.0f];
    _colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 *space, 1.5 *space, 8.0f, 8.0f)];
    _colorLabel.backgroundColor = [UIColor greenColor];
    _colorLabel.layer.cornerRadius = 4.0f;
    _colorLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:_colorLabel];
    
    _numLabel = [ViewTool getLabelWith:CGRectMake(_colorLabel.maxX +5 , space, 200.0f, 20.0f) WithTitle:@" " WithFontSize:15.0f WithTitleColor:GRAYCOLOR WithTextAlignment:NSTextAlignmentLeft];
    
    [self.contentView addSubview:_numLabel];
    
    _nameLable = [ViewTool getLabelWith:CGRectMake( _numLabel.x,_numLabel.maxY + space, [UIView getWidth:120.0f], 20.0f) WithTitle:@" " WithFontSize:15.0f WithTitleColor:BLACKTITLECOLOR WithTextAlignment:NSTextAlignmentLeft];
    [self.contentView addSubview:_nameLable];
    
    
//    CGContextRef contextRef = UIGraphicsGetCurrentContext();
 
    [self addline];
    
    
    _percentLabel = [ViewTool getLabelWith:CGRectMake( kScreenWidth - 70.0f,_numLabel.maxY + space, 70.0, 20.0f) WithTitle:@" " WithFontSize:19.0f WithTitleColor:[UIColor orangeColor] WithTextAlignment:NSTextAlignmentLeft];
    [self.contentView addSubview:_percentLabel];
    
    
}
- (void)addline
{
   _bezier = [UIBezierPath bezierPath];
    [_bezier moveToPoint:CGPointMake([UIView getWidth:120.0f], _nameLable.y + _nameLable.height / 2.0f)];
    [_bezier addLineToPoint:CGPointMake([UIView getWidth:120.0f] + [UIView getWidth:130.0f],  _nameLable.y + _nameLable.height / 2.0f)];
    _bezier.lineWidth = 8.0f;
    _bezier.lineJoinStyle = kCGLineJoinRound;
    UIColor *colo = [UIColor colorWithWhite:0.8 alpha:1];
//    [colo setFill];
    
    CAShapeLayer *backlayer = [CAShapeLayer layer];
    backlayer.strokeColor = colo.CGColor;
    backlayer.lineWidth = 5.0f;
    backlayer.path = _bezier.CGPath;
    backlayer.frame = self.frame;
    backlayer.lineCap = kCALineJoinRound;
    backlayer.lineJoin = kCALineJoinRound;
    [self.layer addSublayer:backlayer];
    
    _shapLayer = [CAShapeLayer layer];
//    shapLayer.fillColor = [UIColor redColor].CGColor;
    _shapLayer.strokeColor = [UIColor orangeColor].CGColor;
    _shapLayer.lineWidth = 5.0f;
    _shapLayer.path = _bezier.CGPath;
    _shapLayer.frame = self.frame;
    _shapLayer.lineCap = kCALineJoinRound;
    _shapLayer.lineJoin = kCALineJoinRound;
    [self.layer addSublayer:_shapLayer];
    
////     *strokeEndAnimation ;
//    _strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//    _strokeEndAnimation.duration = 0.35f;
//    _strokeEndAnimation.fromValue = @(0);
////    _strokeEndAnimation.toValue = @(1);
//    _strokeEndAnimation.autoreverses = NO; //无自动动态倒退效果
//    _strokeEndAnimation.delegate = self;
    
  
    

    
}
- (void)setPrecent:(CGFloat)precent
{
    _precent = precent;
    _strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    _strokeEndAnimation.duration = 0.35f;
    _strokeEndAnimation.fromValue = @(0);
    //    _strokeEndAnimation.toValue = @(1);
    _strokeEndAnimation.autoreverses = NO; //无自动动态倒退效果
    _strokeEndAnimation.delegate = self;
    
    _strokeEndAnimation.toValue = @(precent / 100);
    
    //保持动画的技术效果一下两句要同时写上
     _strokeEndAnimation.removedOnCompletion = NO;
    _strokeEndAnimation.fillMode = kCAFillModeForwards;
    
    [_shapLayer addAnimation:_strokeEndAnimation forKey:@"strokeEnd"];
    _percentLabel.text = [NSString stringWithFormat:@"%.1f%%",precent];
}
- (void)setPrecentColor:(UIColor *)precentColor
{
    _precentColor = precentColor;
    _shapLayer.strokeColor = precentColor.CGColor;
    _percentLabel.textColor = precentColor;
    _colorLabel.backgroundColor = precentColor;

}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    _colorLabel.backgroundColor = self.precentColor;
}
- (void)setNameStr:(NSString *)nameStr{
    _nameStr = nameStr;
    _nameLable.text = _nameStr;
}
- (void)setNumStr:(NSString *)numStr{
    _numStr = numStr;
    _numLabel.text = [NSString stringWithFormat:@"NO:%@",_numStr];
}
- (void)awakeFromNib {
    [super awakeFromNib];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
_colorLabel.backgroundColor = self.precentColor;
    // Configure the view for the selected state
}

@end
